package com.loubobooo.personal.blog.service.impl;

import com.loubobooo.personal.blog.entity.vo.ArticleVO;
import com.loubobooo.personal.blog.entity.vo.TagVO;
import com.loubobooo.personal.blog.mapper.TagMapper;
import com.loubobooo.personal.blog.mapper.custom.ArticleMapperCustom;
import com.loubobooo.personal.blog.mapper.custom.CategoryMapperCustom;
import com.loubobooo.personal.blog.mapper.custom.TagMapperCustom;
import com.loubobooo.personal.blog.entity.Tag;
import com.loubobooo.personal.blog.entity.vo.ArticleListVo;
import com.loubobooo.personal.blog.entity.vo.CategoryVO;
import com.loubobooo.personal.blog.page.PageResult;
import com.loubobooo.personal.blog.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
	@Autowired
	private TagMapper tagMapper;

	@Autowired
	private CategoryMapperCustom categoryMapperCustom;

	@Autowired
	private TagMapperCustom tagMapperCustom;

	@Autowired
	private ArticleMapperCustom articleMapperCustom;

	/**
	  * @Description: 获得标签总数
	  * @return     返回类型
	  * @date 2018/3/23 16:23
	  */
	@Override
	public Integer countTag(Integer status)  {
		Integer tagCount = tagMapperCustom.countTag(status);
		return tagCount;
	}
	
	/**
	  * @Description: 获得标签列表
	  * @return     返回类型
	  * @date 2018/3/23 16:23
	  */
	@Override
	public List<TagVO> listTag(Integer status)  {
		List<TagVO> tagList = tagMapperCustom.listTag(status);
		for(int i=0; i<tagList.size() ;i++) {
			Integer tagId = tagList.get(i).getTagId();
			int count = articleMapperCustom.countArticleByTag(status,tagId);
			tagList.get(i).setArticleCount(count);
		}
		return tagList;
	}

	
	/**
	  * @Description: 获得含有该标签的文章列表
	  * @return     返回类型
	  * @date 2018/3/23 16:23
	  */
	@Override
	public List<ArticleListVo> getArticleListByPage(Integer status,Integer pageNo, Integer pageSize,Integer tagId)  {
		List<ArticleListVo> articleListVoList = new ArrayList<ArticleListVo>();

		//获得该标签的具体信息
		Tag tag = tagMapper.selectByPrimaryKey(tagId);
		if(tag == null) {
			return null;
		}
		
		//分页显示
		PageResult pageResult = null;

		int totalCount = articleMapperCustom.countArticleByTag(status,tagId);
		if (pageNo != null) {
			pageResult = new PageResult(totalCount, pageNo,pageSize);
		} else {
			pageResult = new PageResult(totalCount, 1,pageSize);
		}
		List<ArticleVO> articleVOList = tagMapperCustom.listArticleWithTagByPage(status,tagId,pageResult.getStartPos(), pageResult.getPageSize());
		
		for(int i = 0; i< articleVOList.size(); i++) {
			ArticleListVo articleListVo = new ArticleListVo();
			//1、将文章装入 articleListVo
			ArticleVO articleVO = articleVOList.get(i);
			articleListVo.setArticleVO(articleVO);
			//2、将分类信息装到 articleListVoList 中
			List<CategoryVO> categoryVOList = new ArrayList<CategoryVO>();
			Integer cate = articleVOList.get(i).getArticleParentCategoryId();
			Integer cate2 = articleVOList.get(i).getArticleChildCategoryId();
			CategoryVO categoryVO = categoryMapperCustom.getCategoryById(status,cate);
			CategoryVO categoryVO2 = categoryMapperCustom.getCategoryById(status,cate2);
			if(categoryVO !=null) {
				categoryVOList.add(categoryVO);
			}
			if(categoryVO2 !=null) {
				categoryVOList.add(categoryVO2);
			}
			articleListVo.setCategoryVOList(categoryVOList);
			
			articleListVoList.add(articleListVo);
		}
		//确保该标签有文章，防止空指针
		if(totalCount!=0) {
			//3、将Page存储在 articleListVoList 第一个元素中
			articleListVoList.get(0).setPageResult(pageResult);

			//4、将标签信息 装入  articleListVoList 第一个元素中
			List<TagVO> tagVOList = new ArrayList<TagVO>();
			TagVO tagVO = new TagVO();
			BeanUtils.copyProperties(tag, tagVO);
			tagVOList.add(tagVO);
			articleListVoList.get(0).setTagVOList(tagVOList);
		}
		return articleListVoList;
	}

    @Override
    public TagVO getTagById(Integer id)  {
	    TagVO tagVO = new TagVO();
	    Tag tag = tagMapper.selectByPrimaryKey(id);
	    if(tag != null){
			BeanUtils.copyProperties(tag, tagVO);
			return tagVO;
		}
	    return null;
    }

    @Override
    public void insertTag(Tag tag)  {
        tagMapper.insertSelective(tag);
    }

    @Override
    public void updateTag(Tag tag)  {
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    @Override
    public void deleteTag(Integer id)  {
        tagMapper.deleteByPrimaryKey(id);
    }

	@Override
	public Tag getTagByName(String name)  {
		return tagMapperCustom.getTagByName(name);
	}


}
